package cn.mesmile.amqp.mq;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

import static cn.mesmile.amqp.config.RabbitMQConfig.QUEUE_NAME_TEST;

/**
 * @author zb
 * @date 2021/3/15 21:45
 * @Description:
 */
@Component
//@RabbitListener(queues = "order_queue")
public class TestMQListener {

    /**
     * spring:
     *   rabbitmq:
     *     #开启手动确认消息，如果消息重新入队，进行重试
     *     listener:
     *       simple:
     *         acknowledge-mode: manual
     *
     */

//    @RabbitHandler
    @RabbitListener(queues = QUEUE_NAME_TEST)
    public void messageHandler(String body, Message message, Channel channel) throws IOException {

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        MessageProperties messageProperties = message.getMessageProperties();

        System.out.println("-------------------------------");
        System.out.println("deliveryTag = " + deliveryTag);
        System.out.println("message.toString() = " + message.toString());
        System.out.println("body = " + body);
        System.out.println("messageProperties = " + messageProperties);
        System.out.println("messageProperties.getAppId() = " + messageProperties.getAppId());
        System.out.println("messageProperties.getMessageId() = " + messageProperties.getMessageId());
        System.out.println("messageProperties.getClusterId() = " + messageProperties.getClusterId());
        System.out.println("messageProperties.getCorrelationId() = " + messageProperties.getCorrelationId());
        System.out.println("-------------------------------");

        // 每次消费一条，消费完成后才进行下一条消费
        channel.basicQos(1);
        // 告诉 broker ，消息已经被确认
        channel.basicAck(deliveryTag,false);

        // 告诉，broker，消息拒绝确认   单条确认   重新入队
//        channel.basicNack(deliveryTag,false,true);

        /*
         * basicNack和basicReject介绍
         *
         * basicReject一次只能拒绝接收一个消息，可以设置是否requeue。
         * basicNack方法可以支持一次0个或多个消息的拒收，可以设置是否requeue。
         */

    }



}
